package com.yida.gwj.expend.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.domain.GwjMaintenanceFundsBusinessInner;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountMainRequest;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.expend.domain.GwjMaintenanceFundsBusiness;
import com.yida.gwj.expend.domain.GwjMaintenanceFundsBusinessBuild;
import com.yida.gwj.expend.domain.GwjMaintenanceFundsBusinessHouse;
import com.yida.gwj.expend.domain.dto.AllocatedAmountListDTO;
import com.yida.gwj.expend.domain.dto.Application4ApprovalDTO;
import com.yida.gwj.expend.domain.dto.ApplicationReportDTO;
import com.yida.gwj.expend.domain.dto.ChangeSchemeDTO;
import com.yida.gwj.expend.domain.dto.ConfirmReceiptDTO;
import com.yida.gwj.expend.domain.dto.FilingCertificateDTO;
import com.yida.gwj.expend.domain.dto.GwjMaintenanceFundsBusinessGetInfoDTO;
import com.yida.gwj.expend.domain.dto.GwjMaintenanceFundsBusinessListDTO;
import com.yida.gwj.expend.domain.dto.OneTimeNotificationDTO;
import com.yida.gwj.expend.domain.dto.StepConfigInfoDTO;
import com.yida.gwj.expend.domain.request.AllocatedAmountRequest;
import com.yida.gwj.expend.domain.request.BusinessProcessRequest;
import com.yida.gwj.expend.domain.request.EditFormRequest;
import com.yida.gwj.expend.domain.request.GwjMaintenanceFundsBusinessListRequest;
import com.yida.gwj.expend.domain.request.HousesUnderBuildRequest;
import com.yida.gwj.expend.domain.request.SubmitRequest;
import com.yida.gwj.expend.enums.FlagStatusEnum;
import com.yida.gwj.expend.service.IGwjExpendBaseHouseService;
import com.yida.gwj.expend.service.IGwjMaintenanceFundsBusinessService;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公维金维修资金预受理内网业务 mfb=/maintenance/funds/business
 *
 * @author liangp
 * @date 2023/02/22
 */
@Api(value = "公维金维修资金预受理内网业务", tags = "公维金维修资金预受理内网业务")
@RestController
@RequestMapping("/expend")
public class GwjMaintenanceFundsBusinessController extends BaseController {

    // @Autowired
    // private MqSendMessageHandler mqSendMessageHandler;
    @Autowired
    private IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService;
    // @Autowired
    // private MaintenanceFundsBusinessConfiguration maintenanceFundsBusinessConfiguration;

    @Autowired
    private IGwjExpendBaseHouseService gwjExpendBaseHouseService;

    @ApiOperation(value = "查询公维金维修资金业务列表", response = GwjMaintenanceFundsBusinessListDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/list")
    public TableDataInfo list(@Validated GwjMaintenanceFundsBusinessListRequest maintenanceFundsBusinessListRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessListDTO> list = maintenanceFundsBusinessService.getGwjMaintenanceFundsBusinessList(maintenanceFundsBusinessListRequest);
        return getDataTable(list);
    }

    @ApiOperation(value = "取件")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/take/parts")
    public AjaxResult takeParts(@RequestBody BusinessProcessRequest businessProcessRequest) {
        return toAjax(maintenanceFundsBusinessService.takeParts(businessProcessRequest));
    }

    @ApiOperation(value = "基础业务信息详情", response = GwjMaintenanceFundsBusinessGetInfoDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping(value = "/{bizId}")
    public AjaxResult getInfoDetails(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getInfoDetails(bizId));
    }

    @ApiOperation(value = "分摊范围及金额-选中房屋列表", response = AllocatedAmountListDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @PostMapping("/allocated/amount/list")
    public AjaxResult allocatedAmountList(@RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        return AjaxResult.success(maintenanceFundsBusinessService.allocatedAmountSelect(allocatedAmountRequest));
    }

    @ApiOperation(value = "分摊范围及金额-未选中房屋列表", response = GwjMaintenanceFundsBusinessHouse.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/allocated/amount/cancel/list")
    public TableDataInfo allocatedAmountCancelList(AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> maintenanceFundsBusinessHouseList = maintenanceFundsBusinessService.allocatedAmountCancelList(allocatedAmountRequest);
        return getDataTable(maintenanceFundsBusinessHouseList);
    }

    @ApiOperation(value = "预受理审核")
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping("/pre/accept/examine")
    public AjaxResult preAcceptExamine(@RequestParam("bizId") Long bizId) {
        maintenanceFundsBusinessService.preAcceptExamine(bizId);
        return toAjax(FlagStatusEnum.SUC.getCode());
    }

    @ApiOperation(value = "退件")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/pre/accept/retreat")
    public AjaxResult preAcceptRetreat(@RequestBody BusinessProcessRequest businessProcessRequest) {
        return toAjax(maintenanceFundsBusinessService.preAcceptRetreat(businessProcessRequest));
    }

    @ApiOperation(value = "二次补件审核")
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping(value = "/pre/accept/second/examine")
    public AjaxResult preAcceptSecondExamine(@RequestParam("bizId") Long bizId) {
        return toAjax((maintenanceFundsBusinessService.preAcceptSecondExamine(bizId)));
    }

    @ApiOperation(value = "获取步骤按钮权限信息", response = StepConfigInfoDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping(value = "/get/step/config/info")
    public AjaxResult getStepConfigInfo() {
        return AjaxResult.success((maintenanceFundsBusinessService.getStepConfigInfo(GwjBusinessFlagType.EXPEND_PRE, GwjBusinessFlagType.EXPEND_FORMAL)));
    }

    @ApiOperation(value = "二次补件信息-查看申请报告", response = ApplicationReportDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/get/application/report")
    public AjaxResult getApplicationReport(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getApplicationReport(bizId));
    }

    @ApiOperation(value = "查看改造方案", response = ChangeSchemeDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/get/transformation/plan")
    public AjaxResult getTransformationPlan(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getTransformationPlan(bizId));
    }

    @ApiOperation(value = "确认受理")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/confirm/accept")
    public AjaxResult confirmAccept(@RequestBody SubmitRequest submitRequest) {
        return toAjax(maintenanceFundsBusinessService.confirmAcceptance(submitRequest));
    }

    @ApiOperation(value = "提交")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody SubmitRequest submitRequest) {
        return toAjax(maintenanceFundsBusinessService.submit(submitRequest));
    }

    @ApiOperation(value = "确认收件")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/confirm/receipt")
    public AjaxResult confirmReceipt(@RequestBody ConfirmReceiptDTO confirmReceipt) {
        return toAjax(maintenanceFundsBusinessService.confirmReceipt(confirmReceipt));
    }

    @ApiOperation(value = "回退")
    @RequiresPermissions("gwj:expend:edit")
    @PostMapping("/fallback")
    public AjaxResult fallback(@RequestBody SubmitRequest submitRequest) {
        return toAjax(maintenanceFundsBusinessService.fallback(submitRequest));
    }

    @ApiOperation(value = "取消收件")
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping("/cancel/receipt")
    public AjaxResult cancelReceipt(@RequestParam("attachmentId") Long attachmentId) {
        return toAjax(maintenanceFundsBusinessService.cancelReceipt(attachmentId));
    }

    @ApiOperation(value = "一次性通知单", response = OneTimeNotificationDTO.class)
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping("/one/time/notification")
    public AjaxResult oneTimeNotification(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.oneTimeNotification(bizId));
    }

    @ApiOperation(value = "修改公维金表单信息")
    @RequiresPermissions("gwj:expend:edit")
    @Log(title = "修改公维金表单信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/form")
    public AjaxResult editForm(@Valid @RequestBody EditFormRequest editFormRequest) {
        return AjaxResult.success(maintenanceFundsBusinessService.editForm(editFormRequest));
    }

    @ApiOperation(value = "获取申请报批信息", response = Application4ApprovalDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/get/application/approval")
    public AjaxResult getApplication4Approval(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getApplication4Approval(bizId));
    }

    @ApiOperation(value = "查询已选中楼栋列表")
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/buildList/{bizId}")
    public TableDataInfo getSelectBuilds(@PathVariable("bizId") Long bizId) {
        startPage();
        List<GwjMaintenanceFundsBusinessBuild> selectBuilds = maintenanceFundsBusinessService.getSelectBuilds(bizId);
        return getDataTable(selectBuilds);
    }

    @ApiOperation(value = "获取使用拨付审批表", response = Application4ApprovalDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/get/appropriate/approval")
    public AjaxResult getAppropriate4Approval(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getAppropriate4Approval(bizId));
    }

    @ApiOperation(value = "查询楼栋下房屋列表")
    @RequiresPermissions("gwj:expend:list")
    @PostMapping("/houseList")
    public TableDataInfo getHouseList(@Validated @RequestBody HousesUnderBuildRequest housesUnderBuildRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> houseList = maintenanceFundsBusinessService.getHouseList(housesUnderBuildRequest);
        return getDataTable(houseList);
    }

    @ApiOperation(value = "生成财务流水")
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping("/generate/financial/journal")
    public AjaxResult generateFinancialJournal(@RequestParam("bizId") Long bizId) {
        return toAjax(maintenanceFundsBusinessService.generateFinancialJournal(bizId));
    }

    @ApiOperation(value = "查询总账流水列表", response = GwjFinanceAccountMain.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/accountMain/list")
    public TableDataInfo accountMainList(@Validated RemoteGwjFinanceAccountMainRequest mainRequest) {
        startPage();
        return getDataTable(maintenanceFundsBusinessService.selectGwjFinanceAccountMainList(mainRequest));
    }

    @ApiOperation(value = "导出房屋数据")
    @RequiresPermissions("gwj:expend:export")
    @Log(title = "公维金维修资金业务导出房屋数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        List<AllocatedAmountListDTO> list = maintenanceFundsBusinessService.exportHouseList(allocatedAmountRequest);
        ExcelUtil<AllocatedAmountListDTO> util = new ExcelUtil<>(AllocatedAmountListDTO.class);
        util.exportExcel(response, list, "公维金维修资金业务房屋数据");
    }

    @ApiOperation(value = "打印备案证明", response = FilingCertificateDTO.class)
    @RequiresPermissions("gwj:expend:list")
    @GetMapping("/filing/certificate/{bizId}")
    public AjaxResult printFilingCertificate(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.printFilingCertificate(bizId));
    }

    /**
     * 内部获取公维金维修资金业务
     *
     * @param bizId 业务主键
     * @return
     */
    @ApiOperation(value = "内部获取公维金维修资金业务", response = GwjMaintenanceFundsBusinessInner.class)
    @GetMapping("/innerGetInfo/{bizId}")
    @InnerAuth
    public AjaxResult innerGetInfo(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.innerGetInfo(bizId));
    }

    /**
     * 查询业主征求意见材料-房屋业主投票列表
     *
     * @param bizId 业务ID
     * @return
     */
    @ApiOperation("查询业主征求意见材料-房屋业主投票列表")
    @GetMapping("/get/owner/comments/{bizId}")
    public TableDataInfo selectHouseAndOwnerVoteInfo(@PathVariable("bizId") Long bizId) {
        GwjMaintenanceFundsBusiness maintenanceFundsBusiness = maintenanceFundsBusinessService.selectGwjMaintenanceFundsBusinessByBusinessId(bizId);
        if (ObjectUtil.isNull(maintenanceFundsBusiness)) {
            throw new ServiceException("未获取到对应业务信息！");
        }
        PageUtils.startPage();
        return getDataTable(gwjExpendBaseHouseService.selectHouseAndOwnerVoteInfosByBizIdAndBizNum(bizId, maintenanceFundsBusiness.getBizNum()));
    }

    /**
     * 查询业主征求意见材料-房屋业主投票列表
     *
     * @param outServerIdentification 业务标识
     * @return
     */
    @ApiOperation("查询业主征求意见材料-房屋业主投票列表")
    @GetMapping("/owner/comments/selectHouseAndOwnerVoteInfo/{outServerIdentification}")
    public TableDataInfo selectHouseAndOwnerVoteInfo(@PathVariable("outServerIdentification") String outServerIdentification) {
        PageUtils.startPage();
        return getDataTable(maintenanceFundsBusinessService.selectHouseAndOwnerVoteInfo(outServerIdentification));
    }

    /**
     * 获取申请报批信息-业主表决情况
     *
     * @param outServerIdentification 外网业务标识
     * @return
     */
    @ApiOperation("获取申请报批信息-业主表决情况")
    @GetMapping("/get/application/approval/owner/check/{outServerIdentification}/{bizFlagId}")
    public AjaxResult getApplication4ApprovalOwnerCheck(@PathVariable("outServerIdentification") String outServerIdentification, @PathVariable("bizFlagId") Integer bizFlagId) {
        return AjaxResult.success(maintenanceFundsBusinessService.getApplication4ApprovalOwnerCheck(outServerIdentification, bizFlagId));
    }

    /**
     * 财务对账
     *
     * @param bizId 业务主键
     * @return
     */
    @ApiOperation(value = "财务对账")
    @RequiresPermissions("gwj:expend:edit")
    @GetMapping("/finance/check/{bizId}")
    public AjaxResult financeCheck(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(maintenanceFundsBusinessService.financeCheck(bizId));
    }
}
